// ml:run = $bin cmd.sh
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <err.h>
#include <sys/wait.h>

void run(std::string const& path)
{
    if (auto pid = fork()) {
        if (pid < 0) err(1, "%s", "fork failure");
        std::cerr << "running " << path << " with " << pid << "\n";
        if (waitpid(pid, nullptr, 0) < 0)
            err(3, "failed to wait for %d", pid);
    } else {
        if (execlp(path.c_str(), path.c_str(), nullptr) < 0)
            err(2, "%s", "exec failure");
    }
}

int main(int argc, char* argv[])
{
    if (argc != 2) {
        std::cerr << "USAGE: " << argv[0] << " shell-script\n";
        return 4;
    }
    std::ifstream fin{argv[1]};
    for (std::string cmd; std::getline(fin, cmd); )
        if (cmd != "") run(cmd);
}

